package com.mallcai.bigdata.ladon.controller.system;

import com.cake.bigdata.starter.exception.category.BusinessException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mallcai.bigdata.ladon.controller.system.form.RoleForm;
import com.mallcai.bigdata.ladon.dpl.entity.system.Role;
import com.mallcai.bigdata.ladon.service.system.IRoleService;
import com.mallcai.bigdata.ladon.vo.MyPageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * Created by oneape<oneape15@163.com>
 * Created 2019-06-20 10:34.
 * Modify:
 */
@RequestMapping("/sys/role")
@RestController
public class RoleController {
    @Autowired
    private IRoleService iRoleService;

    @RequestMapping("/add")
    public String addUser(@RequestBody @Validated(value = RoleForm.AddCheck.class) RoleForm form) {
        Role role = form.toEntity();

        int status = iRoleService.addRole(role);
        if (status <= 0) {
            throw new BusinessException("添加角色失败");
        }
        return "添加角色成功";
    }

    @RequestMapping("/edit")
    public String editUser(@RequestBody @Validated(value = RoleForm.EditCheck.class) RoleForm form) {
        Role role = form.toEntity();

        int status = iRoleService.updateRole(role);
        if (status <= 0) {
            throw new BusinessException("修改角色失败");
        }

        return "修改角色成功";
    }

    @RequestMapping("/del")
    public String delUser(@RequestBody @Validated(value = RoleForm.IdCheck.class) RoleForm form) {
        Role role = form.toEntity();
        int status = iRoleService.deleteRole(role);
        if (status <= 0) {
            throw new BusinessException("删除角色失败");
        }
        return "删除角色成功";
    }


    @RequestMapping("/getByPage")
    public MyPageInfo getByPage(@RequestBody @Validated RoleForm form) {
        PageHelper.startPage(form.getCurrentPage(), form.getPageSize());
        List<Role> models = iRoleService.getByParams(form.getParams());
        PageInfo<Role> pageInfo = new PageInfo<>(models);

        return new MyPageInfo<>(pageInfo);
    }

    @RequestMapping("/getAllRoles")
    public List<Role> getAllRoles() {
        List<Role> roles = iRoleService.getAllRoles();
        return roles;
    }

    /**
     * 保存角色的资源权限列表
     *
     * @return ApiResult
     */
    @RequestMapping("/saveRoleRes")
    public String saveRoleRes(@RequestBody @Validated(value = RoleForm.SaveRoleResCheck.class) RoleForm form) {
        int status = iRoleService.saveRoleRes(form.getRoleId(), form.getRoleMaskMap());
        if (status <= 0) {
            throw new BusinessException("保存角色资源权限失败");
        }
        return "保存角色资源权限成功";
    }

    /**
     * 获取指定角色资源权限
     *
     * @param form RoleForm
     * @return Map
     */
    @RequestMapping("/getRoleRes")
    public Map<Long, List<Integer>> getRoleRes(@RequestBody @Validated(value = RoleForm.IdCheck.class) RoleForm form) {
        return iRoleService.getRoleRes(form.getRoleId());
    }

    /**
     * 获取指定角色报表权限
     *
     * @param form RoleForm
     * @return List
     */
    @RequestMapping("/getRoleReport")
    public List<Long> getRoleReport(@RequestBody @Validated(value = RoleForm.IdCheck.class) RoleForm form) {
        return iRoleService.getRoleReport(form.getRoleId());
    }

    /**
     * 保存角色报表权限列表
     *
     * @param form RoleForm
     * @return ApiResult
     */
    @RequestMapping("/saveRoleReport")
    public String saveRoleReport(@RequestBody @Validated(value = RoleForm.SaveRoleReportCheck.class) RoleForm form) {
        int status = iRoleService.saveRoleReport(form.getRoleId(), form.getReportIds());
        if (status <= 0) {
            throw new BusinessException("保存角色报表权限失败");
        }
        return "保存角色报表权限成功";
    }
}
